package com.winit.deploy.task.phpTask;

import com.winit.core.server.base.CmdOutputHandler;
import com.winit.core.task.Task;
import com.winit.deploy.bean.WinitPhpInfo;
import com.winit.deploy.server.WinitPhpServer;
import com.winit.deploy.util.DeployFileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * php备份
 * Created by wangjian on 16/10/17.
 */
public class PhpBakTask extends Task{

    private static Logger logger = LoggerFactory.getLogger(PhpBakTask.class);

    private WinitPhpServer phpServer;

    public PhpBakTask(String taskId, Integer expireTime,WinitPhpServer phpServer) {
        super(taskId, expireTime);
        this.phpServer = phpServer;
    }

    @Override
    public TaskStatus call() throws Exception {
        logger.info("com.winit.deploy.task.phpTask.PhpBakTask.call start taskId="+this.taskId);
        final TaskStatus[] taskStatus = {TaskStatus.STATUS_SUCCESS};
        CmdOutputHandler handler = new CmdOutputHandler(){

            @Override
            public void onMessage(String cmd, int index, String message, Boolean isErrorMessage) {
                if(isErrorMessage){
                    taskStatus[0] = TaskStatus.STATUS_ERROR;
                }else{
                    taskStatus[0] = TaskStatus.STATUS_SUCCESS;
                }
                logger.info("PhpBakTask:执行备份,isErrorMessage="+isErrorMessage+",message="+message);
            }
        };
        this.phpServer.setCmdOutputHandler(handler);
        this.phpServer.doBakUp();
        WinitPhpInfo info = this.phpServer.getPhpInfo();
        DeployFileUtil.saveBackPackagePath(info.getEnvName(),info.getAreaName(),this.taskId,info.getBackUpPath());
        logger.info("com.winit.deploy.task.phpTask.PhpBakTask.call end taskId="+this.taskId);
        return taskStatus[0];
    }
}
